package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.AjaxResult;
import com.sofwin.Constants;
import com.sofwin.system.pojo.SysUser;
import com.sofwin.system.service.UserService;
import com.sofwin.system.vo.UserVO;
import com.sofwin.util.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/7/13 10:47
 * @email 115176513@qq.com
 * @description: 用于的请求
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    // 返回分页数据的请求
    @GetMapping("data")
    public PageInfo data(  UserVO user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = "10") Integer pageSize){
        PageInfo page =  userService.queryUserByPage(user,pageNumber,pageSize);
        return page;
    }

    // 定义返回用户信息的请求

    // 用户保存的请求
    @PostMapping("save")
    public Map save(SysUser user){
        user.setCreateDate(DateUtil.getCurrentDateStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        Map map = new HashMap();
        boolean status = userService.saveOrUpdateUser(user);
        map.put("status",status);
        if(status){
            map.put("msg","操作成功");
        }else {
            map.put("msg","操作失败");
        }

        return map;
    }

    // 用户删除的请求
    @GetMapping("delete")
    public Map delete(Integer id){
        Map map  = new HashMap();
        boolean status = userService.removeUserById(id);
        map.put("status",status);
        if(status){
            map.put("msg","删除成功");
        }else{
            map.put("msg","删除失败");
        }
        return map;
    }

    @RequestMapping("deletes")
    public AjaxResult deletes(@RequestParam(name = "ids[]") Integer[] ids, @RequestParam(name = "ids[]") List<Integer> ids2){
        boolean status = userService.removeUserByIds(ids);
        if(status){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }

    }

    @PostMapping("login")
    public AjaxResult login(SysUser user, HttpSession session,@SessionAttribute(name = "xdsdsd") String a ,  String code, HttpServletRequest request){
        Object obj = session.getAttribute(Constants.KAP_CODE);
        HttpSession session1 = request.getSession();
        Object obj2 = session1.getAttribute(Constants.KAP_CODE);
        String mcode = session.getAttribute(Constants.KAP_CODE).toString();
        if(!code.equals(mcode)){
            return AjaxResult.error("验证码错误");
        }
        Subject subject = SecurityUtils.getSubject();// 创建一个登陆主体
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginAccount(),user.getLoginPwd());
        try {
            subject.login(token);// 执行登陆过程
            user.setRoleId(1);
            session.setAttribute(Constants.CURR_USER,user);
            return AjaxResult.success();
        }catch (UnknownAccountException ex){
            return  AjaxResult.error("用户名不存在");
        }catch (AuthenticationException ex){
            return AjaxResult.error("密码错误");
        }
    }


}
